/*================================================================*
        Copyright (C) 2021 All rights reserved www.hsby.link
      	�ļ����ƣ�test_socket.cpp
      	�� �� �ߣ���ɭ����
      	�������ڣ�2021/12/15
 *================================================================*/

#include <sys/fcntl.h>
#include "socket.h"
#include "util/util.h"
#include "iomanager.h"

void TestConnect() {
    // ����Զ�˵�ַ
    auto remote_addr = std::dynamic_pointer_cast<hsby::IpAddress>(hsby::BaseAddress::LookUpAnyIpAddress("127.0.0.1", AF_INET, SOCK_STREAM));
    if (!remote_addr) {
        //HSBY_LOG_SYSTEM_DEBUG << HSBY_ERROR_EXP;
        return;
    }
    remote_addr->SetPort(10086);

    auto sock = hsby::Socket::CreateTCP(remote_addr);

    // ��������
    if (!sock->Connect(remote_addr)) {
        HSBY_LOG_SYSTEM_DEBUG << HSBY_ERROR_EXP;
        return;
    }
    HSBY_LOG_SYSTEM_DEBUG << "Connect success! ";

    // ����
    std::string send_buf;
    while(std::cin >> send_buf) {
        sock->Send(send_buf);
        std::string recv_buf;
        int recv_size = sock->Recv(recv_buf);
        HSBY_LOG_SYSTEM_DEBUG << HSBY_VARS_EXP(recv_size, recv_buf);
    }
}

int main() {
    hsby::IoManager iom;
    iom.AddTask(TestConnect);
    return 0;
}